﻿BuildSubMenus = function () {
    if (document.getElementById("menu") != null) {
        var ultags = document.getElementById("menu").getElementsByTagName("ul");
        for (var t = 0; t < ultags.length; t++) {
            if (ultags[t].parentNode.parentNode.id == "menu") {
                ultags[t].style.top = ultags[t].parentNode.offsetHeight + "px"
            } else {
                ultags[t].style.left = ultags[t - 1].getElementsByTagName("a")[0].offsetWidth + "px";
            }

            ultags[t].parentNode.onmouseover = function () {
                this.getElementsByTagName("ul")[0].style.visibility = "visible";
                $(this).addClass("selected");
            }
            ultags[t].parentNode.onmouseout = function () {
                this.getElementsByTagName("ul")[0].style.visibility = "hidden";
                $(this).removeClass("selected");
            }
        }
    }
}

$(".menu").ready(function () {
    BuildSubMenus();
});

function resize() {
    $("img").each(function (idx, elm) {
        $("<img>").attr("src", elm.src);
    });

    var iOffSet = ($.browser.msie) ? 0 : 20;
    var iW = $(document).width();
    var iH = $(document.body).height() + iOffSet;

    // adjust size
    parent.$.fn.colorbox.myResize(iW, iH);
}

jQuery.ParseJsonDateTime = function(jsonDateTime) {
    var strValue = jsonDateTime.match('[0-9]{1,}');
    var intValue = parseInt(strValue);
    var realDate = new Date(intValue);

    realDate = realDate.getDate() + "/" + (realDate.getMonth() + 1) + "/" + realDate.getFullYear() + " " + realDate.getHours() + ":" + realDate.getMinutes();
    return realDate;
};

jQuery.ParseJsonDate = function(jsonDateTime) {
    var strValue = jsonDateTime.match('[0-9]{1,}');
    var intValue = parseInt(strValue);
    var realDate = new Date(intValue);

    realDate = realDate.getDate() + "/" + (realDate.getMonth() + 1) + "/" + realDate.getFullYear();
    return realDate;
};

var addthis_config = {
    ui_language: "fr"
}

jQuery(function ($) {
    $.datepicker.regional['fr'] = {
        closeText: 'Fermer',
        prevText: '&#x3c;Préc',
        nextText: 'Suiv&#x3e;',
        currentText: 'Courant',
        monthNames: ['Janvier', 'F&eacute;vrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Ao&ucirc;t', 'Septembre', 'Octobre', 'Novembre', 'D&eacute;cembre'],
        monthNamesShort: ['Jan', 'F&eacute;v', 'Mar', 'Avr', 'Mai', 'Jun', 'Jul', 'Ao&ucirc;', 'Sep', 'Oct', 'Nov', 'D&eacute;c'],
        dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
        dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
        dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
        dateFormat: 'dd/mm/yy', firstDay: 1,
        isRTL: false
    };
    $.datepicker.setDefaults($.datepicker.regional['fr']);
});

jQuery.fn.slowEach = function (interval, callback, callback2) {
    var items = this, i = 0;
    if (!items.length) return;
    function next() {
        (callback.call(items[i], i, items[i]) !== false && ++i < items.length) ? setTimeout(next, interval) : callback2 && callback2.call(items, i, items);
    }
    next();
};

// Home / Index / LoadFeed
function LoadFeed() {
    $.getJSON("/Home/FeedReader.json", function (data) {
        $.template("externalFeedTemplateCompiled", $("#externalFeedTemplate"));
        var $UL = $("<ul>").hide();
        $.tmpl("externalFeedTemplateCompiled", data, {
            ConvertToDate: function (item) {
                return $.ParseJsonDate(item.data.Pubdate);
            }
        }).appendTo($UL);

        $UL.appendTo("#fedeList").fadeTo("slow", "1");

        $("#fedeList").removeClass("loader");

        $("#fedeList a").live("click", function () {
            _gaq.push(['_trackEvent', 'News Fédé', 'Click', 'Home_News']);
        });
    });
}

// Members / Manage / CreateUsersList
function CreateUsersList(userList) {

    $("#userList table tr[class!='js-userListHeader']").remove();
    $("#userList table").show();

    $.tmpl("membersTableTemplateCompiled", userList, {
        GetZebra: function (item) {
            return (item.data.Index % 2 == 0) ? "blue" : "white";
        },
        GetIndex: function (item) {
            return item.data.Index;
        },
        GetUserStatus: function (item) {
            if (item.data.IsLocked == "true" && item.data.IsApprouved == "true") {
                return "locked";
            }
            else if (item.data.IsLocked == "false" && item.data.IsApprouved == "false") {
                return "approuved";
            }else {
                return "valide";
            }
        }
    }).appendTo("#userList table");

    $.colorbox.remove();

    $("table a.editUser").colorbox({ width: "400px", height: "370px", scrolling: true, iframe: true, title: "Editer cet utilisateur" });
    $("table a.deleteUser").colorbox({ width: "400px", height: "200px", scrolling: false, iframe: true, title: "Supprimer cet utilisateur" });

    $("#userList .status").simpletip({ fixed: true, content: 
        function () {
            return "aaa";
        } 
    });

    $("#userList").removeClass("loader");
}

/* Email checker */
var defaultMailCount = 0;

function LoadEmailCount() {
    if ($("#emailLink").length == 0) {
        $.ajax({
            type: "POST",
            url: "/Global/UnreadMailCount",
            success: function (mailCount) {
                defaultMailCount = mailCount;
                $("#connect div #statsLink").parent().after('<span><a href="/Administration/Email" id="emailLink">Emails ( <span>' + mailCount + '</span> )</a></span>');
                $.timer(20000, function (timer) {
                    UpdateEmailCount();
                });
            }
        });
    } else {
        defaultMailCount = parseInt($("#connect div #emailLink span").text());
        $.timer(30000, function (timer) {
            UpdateEmailCount();
        });
    }
}

function UpdateEmailCount() {
    $.ajax({
        type: "POST",
        url: "/Global/UnreadMailCount",
        success: function (mailCount) {
            if (defaultMailCount != mailCount) {
                defaultMailCount = mailCount;
                $("#emailLink span").text(mailCount);
                $("#emailLink").effect("pulsate", { times:3 });
            }
        }
    });
}

function displayError(errorMessage) {
    return $('<div class="errorContent"><span>' + errorMessage + '</span></div>');
}